package com.zmm.common.base.model;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonIgnore;

import java.io.Serializable;

/**
 * 操作响应返回对象
 * @author 900045
 * @description:
 * @name ResponseResult
 * @date By 2021-06-22 10:22:46
 */
public class ResponseResult<T> implements Serializable {
	
	private static final long serialVersionUID = -6297406327595997791L;

	/**
	 * 返回码模型，不序列化到前端
	 */
	@JsonIgnore
	private ResponseCode respCode = ResultCode.SUCCESS;

	/**
	 * 返回错误描述   正式环境下可能不返回
	 */
	private String error;

	/** 错误日志唯一标识 */
	private String errorCode;

	private Integer httpStatus;

	/**
	 * 返回数据
	 */
	public T data;

	/**
	 * 扩展数据
	 */
	public Object exData;

	/**
	 * 操作时间
	 */
	private Long nowTime = System.currentTimeMillis();

	/**
	 *  空列表集
	 */
	public static JSONArray blankList = new JSONArray();

	/**
	 *  空对象
	 */
	public static JSONObject blankObj = new JSONObject();

	/**
	 *  是否成功
	 *
	 * @return
	 */
	public boolean isSuccess() {
		return this.respCode == ResultCode.SUCCESS || this.respCode == ResultCode.REQUEST_SUCCESS;
	}

	public ResponseResult() {
	}

	public ResponseResult(T data) {
		this.data = data;
	}


	public ResponseResult(ResponseCode respCode) {
		this.setResultCode(respCode);
	}

	public void setResultCode(ResponseCode respCode) {
		this.respCode = respCode;
	}

	public ResponseResult(ResponseCode respCode, T msg) {
		this.setResultCode(respCode, msg);
	}

	public void setResultCode(ResponseCode respCode, T msg) {
		this.respCode = respCode;
		if (this.getError() == null) {
			this.setError(new StringBuilder().append(respCode).append(":")
					.append(respCode.getDesc()).toString());
			this.setData(msg);
		}
	}

	/**
	 * 获取返回码
	 * @return
	 */
	public Integer getResultCode() {
		return respCode.getCode();
	}

	/**
	 * 获取返回描述
	 * @return
	 */
	public String getDesc(){
		return respCode.getDesc();
	}


	public String getError() {
		return error;
	}

	public void setError(String error) {
		this.error = error;
	}

	public String getErrorCode() {
		return errorCode;
	}

	public void setErrorCode(String errorCode) {
		this.errorCode = errorCode;
	}

	public Integer getHttpStatus() {
		return httpStatus;
	}

	public void setHttpStatus(Integer httpStatus) {
		this.httpStatus = httpStatus;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

	public Long getNowTime() {
		return nowTime;
	}

	public void setNowTime(Long nowTime) {
		this.nowTime = nowTime;
	}
}
